﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;

namespace Comm
{
    public class TBClass
    {
        /// <summary>
        /// 绑定DrpDownList
        /// </summary>
        /// <param name="drp">下拉列表控件</param>
        /// <param name="list">类型集合</param>
        /// <param name="classIdName">类型ID属性名称</param>
        /// <param name="parentIdName">父类型ID属性名称</param>
        /// <param name="className">类型属性名称</param>
        public static void BindDrpDownList<T>(System.Web.UI.WebControls.DropDownList drp, List<T> list, string classIdName, string parentIdName, string className)
        {
            foreachList<T>(drp, list, 0, 0, classIdName, parentIdName, className);
        }

        /// <summary>
        /// 绑定DrpDownList
        /// </summary>
        /// <param name="drp">下拉列表控件</param>
        /// <param name="list">类型集合</param>
        /// <param name="parnetId">父类型ID</param>
        /// <param name="count">深度</param>
        /// <param name="classIdName">类型ID属性名称</param>
        /// <param name="parentIdName">父类型ID属性名称</param>
        /// <param name="className">类型属性名称</param>
        public static void BindDrpDownList<T>(System.Web.UI.WebControls.DropDownList drp, List<T> list, int parentId, int count, string classIdName, string parentIdName, string className)
        {
            foreachList<T>(drp, list, parentId, count, classIdName, parentIdName, className);
        }

        /// <summary>
        /// 循环绑定
        /// </summary>
        /// <param name="drp">下拉列表控件</param>
        /// <param name="list">类型集合</param>
        /// <param name="parnetId">父类型ID</param>
        /// <param name="count">深度</param>
        /// <param name="classIdName">类型ID属性名称</param>
        /// <param name="parentIdName">父类型ID属性名称</param>
        /// <param name="className">类型属性名称</param>
        private static void foreachList<T>(System.Web.UI.WebControls.DropDownList drp, List<T> list, int parnetId, int count, string classIdName, string parentIdName, string className)
        {
            count++;
            string str = "";
            for (int i = 1; i < count; i++) { str += HttpContext.Current.Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); }
            List<T> list1 = list.FindAll(i => Convert.ToInt32(i.GetType().GetProperty(parentIdName).GetValue(i, null)) == parnetId);
            foreach (T m in list1)
            {
                drp.Items.Add(new ListItem(str + m.GetType().GetProperty(className).GetValue(m, null).ToString(), m.GetType().GetProperty(classIdName).GetValue(m, null).ToString()));
                foreachList<T>(drp, list, Convert.ToInt32(m.GetType().GetProperty(classIdName).GetValue(m, null)), count, classIdName, parentIdName, className);
            }
        }

        /// <summary>
        /// 绑定新的List集合
        /// </summary>
        /// <param name="list">类型集合</param>
        /// <param name="classIdName">类型ID属性名称</param>
        /// <param name="parentIdName">父类型ID属性名称</param>
        /// <param name="className">类型属性名称</param>
        public static List<T> BindList<T>(List<T> list, string classIdName, string parentIdName, string className)
        {
            return BindList<T>(list, 0, 0, classIdName, parentIdName, className);
        }

        /// <summary>
        /// 绑定新的List集合
        /// </summary>
        /// <param name="list">类型集合</param>
        /// <param name="parnetId">父类型ID</param>
        /// <param name="count">深度</param>
        /// <param name="classIdName">类型ID属性名称</param>
        /// <param name="parentIdName">父类型ID属性名称</param>
        /// <param name="className">类型属性名称</param>
        public static List<T> BindList<T>(List<T> list, int parentId, int count, string classIdName, string parentIdName, string className)
        {
            List<T> newlist = new List<T>();
            foreachList<T>(newlist, list, parentId, count, classIdName, parentIdName, className);
            return newlist;
        }

        /// <summary>
        /// 循环创建List
        /// </summary>
        /// <param name="drp">新的List集合</param>
        /// <param name="list">类型集合</param>
        /// <param name="parnetId">父类型ID</param>
        /// <param name="count">深度</param>
        /// <param name="classIdName">类型ID属性名称</param>
        /// <param name="parentIdName">父类型ID属性名称</param>
        /// <param name="className">类型属性名称</param>
        private static void foreachList<T>(List<T> newlist, List<T> list, int parnetId, int count, string classIdName, string parentIdName, string className)
        {
            count++;
            string str = "";
            for (int i = 1; i < count; i++) { str += HttpContext.Current.Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); }
            List<T> list1 = list.FindAll(i => Convert.ToInt32(i.GetType().GetProperty(parentIdName).GetValue(i, null)) == parnetId);
            foreach (T m in list1)
            {
                m.GetType().GetProperty(className).SetValue(m, str + m.GetType().GetProperty(className).GetValue(m, null).ToString(), null);
                newlist.Add(m);
                foreachList<T>(newlist, list, Convert.ToInt32(m.GetType().GetProperty(classIdName).GetValue(m, null)), count, classIdName, parentIdName, className);
            }
        }

    }
}
